package com.mt.restaurant.service.staff.impl;

import com.mt.common.core.utils.DateUtils;
import com.mt.restaurant.dao.staff.StaffAttendanceRecordsDao;

import com.mt.common.core.exception.BusinessException;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import com.mt.common.core.web.base.BaseEntity;
import com.mt.common.core.web.BaseService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.mt.restaurant.entity.staff.StaffAttendanceRecords;
import com.mt.restaurant.service.staff.StaffAttendanceRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class StaffAttendanceRecordsServiceBean extends BaseService implements StaffAttendanceRecordsService {

    private static Logger logger = LogManager.getLogger();

	@Autowired
	private StaffAttendanceRecordsDao staffAttendanceRecordsDao;

	@Resource
	private RedisTemplate<String, List<StaffAttendanceRecords>> redisTemplate;

	@Autowired
	StringRedisTemplate stringRedisTemplate;

	/**
	 * 根据分页参数查询学生信息集合
	 *
	 * @param pageDTO 分页条件
	 */
	@Override
	public PageResultDTO findStaffAttendanceRecordss(PageDTO pageDTO){
        pageDTO.setStartIndex((pageDTO.getCurrentPage()-1)*pageDTO.getPageSize());
		//TODO:请在此校验参数的合法性
		this.validateFindStaffAttendanceRecordss(pageDTO);
		List<StaffAttendanceRecords> staffAttendanceRecordsDTOS = this.staffAttendanceRecordsDao.findStaffAttendanceRecordss(pageDTO);
		Long totalCount = this.staffAttendanceRecordsDao.findStaffAttendanceRecordsTotalCount(pageDTO);

		PageResultDTO pageResultDTO = new PageResultDTO();
		pageResultDTO.setTotalCount(totalCount);
		pageResultDTO.setDatas(staffAttendanceRecordsDTOS);

		return pageResultDTO;
	}

	/**
	 * 查询全部学生信息集合
	 *
	 */
	@Override
	public List<StaffAttendanceRecords> findAllStaffAttendanceRecordss(){
		return this.staffAttendanceRecordsDao.findAllStaffAttendanceRecordss();
	}

	/**
	 * 查询所有学生信息集合(只提取ID 和 Name)
	 *
	 */
	@Override
	public List<StaffAttendanceRecords> findAllStaffAttendanceRecordssWithIdName(){
		//TODO:请在此校验参数的合法性
		this.validateFindAllStaffAttendanceRecordssWithIdName();
		return this.staffAttendanceRecordsDao.findAllStaffAttendanceRecordssWithIdName();
	}

	/**
	 * 根据名称查询学生信息集合(只提取ID 和 Name)
	 *
	 * @param staffAttendanceRecordsName 名称
	 */
	@Override
	public List<StaffAttendanceRecords> findStaffAttendanceRecordssWithIdNameByName(String staffAttendanceRecordsName){
		//TODO:请在此校验参数的合法性
		this.validateFindStaffAttendanceRecordssWithIdNameByName(staffAttendanceRecordsName);
		//TODO:缓存取对应参数
		Set<String> keys = stringRedisTemplate.keys("searchData:StaffAttendanceRecords_where_staffAttendanceRecordsName_" + staffAttendanceRecordsName);
		List<StaffAttendanceRecords> staffAttendanceRecordss = new ArrayList<>();
		if (keys.isEmpty()) {
		staffAttendanceRecordss = this.staffAttendanceRecordsDao.findStaffAttendanceRecordssWithIdNameByName(staffAttendanceRecordsName);
		redisTemplate.opsForValue().set("searchData:StaffAttendanceRecords_where_staffAttendanceRecordsName_" + staffAttendanceRecordsName, staffAttendanceRecordss, 30, TimeUnit.DAYS);
		} else {
		staffAttendanceRecordss = redisTemplate.opsForValue().get("searchData:StaffAttendanceRecords_where_staffAttendanceRecordsName_" + staffAttendanceRecordsName);
		}
		return staffAttendanceRecordss;
	}

	/**
	 * 根据ID查询指定的学生信息(只提取ID 和 Name)
	 *
	 * @param staffAttendanceRecordsId Id
	 */
	@Override
	public StaffAttendanceRecords findStaffAttendanceRecordssWithIdNameById(Long staffAttendanceRecordsId){
		//TODO:请在此校验参数的合法性
		this.validateFindStaffAttendanceRecordssWithIdNameById(staffAttendanceRecordsId);
		return this.staffAttendanceRecordsDao.findStaffAttendanceRecordssWithIdNameById(staffAttendanceRecordsId);
	}

	/**
	 * 根据ID查询指定的学生信息
	 *
	 * @param staffAttendanceRecordsId Id
	 */
	@Override
	public StaffAttendanceRecords findStaffAttendanceRecords(Long staffAttendanceRecordsId){
		//TODO:请在此校验参数的合法性
		this.validateFindStaffAttendanceRecords(staffAttendanceRecordsId);
		return this.staffAttendanceRecordsDao.findStaffAttendanceRecords(staffAttendanceRecordsId);
	}

	/**
	 * 根据ID查询指定的学生信息(包含外键)
	 *
	 * @param staffAttendanceRecordsId Id
	 */
	@Override
	public StaffAttendanceRecords findStaffAttendanceRecordsWithForeignName(Long staffAttendanceRecordsId){
		//TODO:请在此校验参数的合法性
		this.validateFindStaffAttendanceRecordsWithForeignName(staffAttendanceRecordsId);
		return this.staffAttendanceRecordsDao.findStaffAttendanceRecordsWithForeignName(staffAttendanceRecordsId);
	}

	/**
	 * 新增学生信息
	 *
	 * @param staffAttendanceRecords 实体对象
	 */
	@Override
	public StaffAttendanceRecords saveStaffAttendanceRecords(StaffAttendanceRecords staffAttendanceRecords){
		//TODO:请在此校验参数的合法性
		this.validateSaveStaffAttendanceRecords(staffAttendanceRecords);
		//TODO:填充公共参数
		this.setSavePulicColumns(staffAttendanceRecords);
		Long rows = this.staffAttendanceRecordsDao.saveStaffAttendanceRecords(staffAttendanceRecords);
		if(rows != 1)
		{
			String error = "新增保存学生信息出错，数据库应该返回1,但返回了 "+rows;
			throw new BusinessException(error);
		}
		return staffAttendanceRecords;
	}

	/**
	 * 更新学生信息
	 *
	 * @param staffAttendanceRecords 实体对象
	 */
	@Override
	public StaffAttendanceRecords updateStaffAttendanceRecords(StaffAttendanceRecords staffAttendanceRecords){
		//TODO:请在此校验参数的合法性
		this.validateUpdateStaffAttendanceRecords(staffAttendanceRecords);
		Long rows = this.staffAttendanceRecordsDao.updateStaffAttendanceRecords(staffAttendanceRecords);
		if(rows != 1)
		{
			String error = "修改保存学生信息出错，数据库应该返回1,但返回了 "+rows+",数据可能被删除";
			throw new BusinessException(error);
		}
		return staffAttendanceRecords;
	}

	/**
	 * 根据ID删除学生信息
	 *
	 * @param staffAttendanceRecordsId ID
	 */
	@Override
	public void deleteStaffAttendanceRecords(Long staffAttendanceRecordsId){
		//TODO:请在此校验参数的合法性
		this.validateDeleteStaffAttendanceRecords(staffAttendanceRecordsId);

		Map<Class<? extends BaseEntity>,EntityUsage> entityUsageMap = this.checkForeignEntity(StaffAttendanceRecords.class, staffAttendanceRecordsId);
		if(entityUsageMap != null && entityUsageMap.size() >0){
			StringBuilder errors = new StringBuilder();
			errors.append("计划删除的数据正在被以下数引用\n");
			for(EntityUsage entityUsage : entityUsageMap.values()){
				errors.append("\t").append(entityUsage.getEntityLabel()).append("\n");
				for(Map.Entry<Long,String> entry : entityUsage.getUsageIdNames().entrySet() ){
					errors.append("\t\t").append(entry.getKey()).append("\t").append(entry.getValue()).append("\n");
				}
			}
			errors.append("，不能删除，请检查处理后再删除");
			throw  new BusinessException(errors.toString());
		}

		Long rows = this.staffAttendanceRecordsDao.deleteStaffAttendanceRecords(staffAttendanceRecordsId);
		if(rows != 1){
			String error = "删除学生信息出错，数据可能已经被删除";
			throw new BusinessException(error);
		}
	}

	//TODO:---------------验证-------------------

	private void validateFindStaffAttendanceRecordss(PageDTO pageDTO) {
	//TODO:请使用下面方法添加数据过滤条件
	//		pageDTO.addFilter("creatorId",this.getLoginUserId());
	//TODO:请完善数据校验规则和数据权限判断，如果有问题请抛出异常，参看下面validateUpdateStaffAttendanceRecords()写法
	}

	private void validateFindStaffAttendanceRecordssWithIdNameByName(String staffAttendanceRecordsName) {
	//TODO:请完善数据校验规则和数据权限判断，如果有问题请抛出异常，参看下面validateUpdateStaffAttendanceRecords()写法
	}


	private void validateFindAllStaffAttendanceRecordssWithIdName() {
	//TODO:请完善数据校验规则和数据权限判断，如果有问题请抛出异常，参看下面validateUpdateStaffAttendanceRecords()写法
	}

	private void validateFindStaffAttendanceRecordssWithIdNameById(Long staffAttendanceRecordsId) {
	//TODO:请完善数据校验规则和数据权限判断，如果有问题请抛出异常，参看下面validateUpdateStaffAttendanceRecords()写法
	}

	private void validateFindStaffAttendanceRecords(Long staffAttendanceRecordsId) {
	//TODO:请完善数据校验规则和数据权限判断，如果有问题请抛出异常，参看下面validateUpdateStaffAttendanceRecords()写法
	}

	private void validateFindStaffAttendanceRecordsWithForeignName(Long staffAttendanceRecordsId) {
	//TODO:请完善数据校验规则和数据权限判断，如果有问题请抛出异常，参看下面validateUpdateStaffAttendanceRecords()写法
	}

	private void validateSaveStaffAttendanceRecords(StaffAttendanceRecords staffAttendanceRecords) {
	//不为空判断
	if (staffAttendanceRecords.getEid() != null || staffAttendanceRecords.getCreatorId() != null || staffAttendanceRecords.getCreateDatetime() != null) {
	throw new BusinessException("非法请求");
	}
	//TODO:请完善数据校验规则和数据权限判断，如果有问题请抛出异常，参看下面validateUpdateStaffAttendanceRecords()写法
	}

	private void validateUpdateStaffAttendanceRecords(StaffAttendanceRecords staffAttendanceRecords) {
	//不为空判断
	if (staffAttendanceRecords.getEid() == null) {
	throw new BusinessException("唯一标识不能为空");
	}
	//是否存在判断
	if (this.staffAttendanceRecordsDao.findStaffAttendanceRecordsTotalCount(PageDTO.create(StaffAttendanceRecords.FIELD_ID, staffAttendanceRecords.getEid())) == 0) {
	throw new BusinessException("修改的学生信息 " + staffAttendanceRecords.getName() + " 不存在，修改失败，请重试或联系管理员");
	}
	//TODO:请完善数据校验规则和数据权限判断，如果有问题请抛出异常，参看下面validateUpdateStaffAttendanceRecords()写法
	}

	private void validateDeleteStaffAttendanceRecords(Long staffAttendanceRecordsId) {
	//TODO:请完善数据校验规则和数据权限判断，如果有问题请抛出异常，参看下面validateUpdateStaffAttendanceRecords()写法
	}

	@Override
	public boolean canDownloadAttachment(String formName, Long id) {
	return true;
	}

	@Override
	public List<StaffAttendanceRecords> findAllStaffAttendanceRecordsByStaffIdAndDate(Long staffId, Date startDate, Date endDate) {

		return this.staffAttendanceRecordsDao.findAllStaffAttendanceRecordsByStaffIdAndDate(staffId,DateUtils.dateTime(startDate),DateUtils.dateTime(endDate));
	}
}
